$(function () {
  // nav状态栏
  let dom = $('.search_nav li')
  dom.each((index, item) => {
    item.onclick = function () {
      dom.each((index, it) => {
        $(it).removeClass('search_nav_cl')
      })
      $(item).addClass('search_nav_cl')
      switch (item.innerHTML) {
        case '单曲':
          se_search(1)
          break;
        case '歌手':
          se_search(100)
          break;
        case '专辑':
          se_search(10)
          break;
        case 'MV':
          se_search(1004)
          break;
        case '视频':
          se_search(1014)
          break;
        case '歌词':
          se_search(1006)
          break;
        case '歌单':
          se_search(1000)
          break;
        case '声音主播':
          se_search(1009)
          break;
        case '用户':
          se_search(1002)
          break;
        default:
          break;
      }
    }
  })
})

//搜索事件
async function se_search(type, offset = 0) {
  if (!type) {
    $('.search_nav li')[0].click()
    return
  }
  var search_text = $('.se_input').val()
  if (!search_text) {
    return
  }
  $('.search_ct')[0].innerText = search_text;
  $('#se_list')[0].innerHTML = '';
  res = await axios({
    url: `/cloudsearch?limit=30&offset=${offset}&keywords=${search_text}&type=${type}`,
    method: 'get'
  })
  var totalNum=0
  if (type == 1) {
    totalNum = res.data.result.songCount; //总数
    let songlist = res.data.result.songs;
    $('#se_list').html()
    songlist.forEach((item, index) => {
      $('#se_list')[0].innerHTML += `
        <tr class="se_video fx_sb">
            <td class="fx_fs fx_3">
                <div class="play_icon" onclick="play_icon_cl(${index},${item.id},${item.fee})"></div>
                <span>${item.name}</span>
            </td>
            <td class="se_actions fx_1 fx_sa">
            </td>
            <td class=" fx_1">
                ${item.ar[0].name}
            </td>
            <td class=" fx_1"><span>${item.al.name}</span></td>
            <td class="toMinutes fx_1"><span>${toMinutes(item.dt)}</span></td>
        </tr>
      `
    })
  } else if (type == 100) {
    totalNum = res.data.result.artistCount; //总数
    let artists = res.data.result.artists;
    $('#se_list').html()
    artists.forEach((item, index) => {
      $('#se_list')[0].innerHTML += `
          <dl class="search_singer f-thide fl">
              <dt>
                  <img src="${item.picUrl}?param=130y130" alt="">
              </dt>
              <dd class="fx_sb">
                  <div class="singer_name ">${item.name}</div>
                  <div class="singer_red"></div>
              </dd>
          </dl>
          `
    })
  } else if (type == 10) {
    totalNum = res.data.result.albumCount; //总数
    let albums = res.data.result.albums;
    $('#se_list').html()
    albums.forEach((item, index) => {
      $('#se_list')[0].innerHTML += `
          <dl class="search_zj fl">
              <dt>
                  <img src="${item.picUrl}?param130y130" alt="">
              </dt>
              <dd>
                  <div class="search_zj_name c_000 f14 f-thide">${item.name}</div>
                  <div class="c_666 f12">${item.artist.name}</div>
              </dd>
          </dl>
          `
    })
  } else if (type == 1014) {
    totalNum = res.data.result.videoCount; //总数
    let videos = res.data.result.videos;
    $('#se_list').html()
    videos.forEach((item, index) => {
      $('#se_list')[0].innerHTML += `
          <dl class="search_videos fl" onclick="play_mv('${item.vid}')">
              <dt>
                  <img src="${item.coverUrl}?param130y130" alt="">
                  <div class="search_videos_num"><span class="search_videos_icon"></span> ${toWan(item.playTime)}</div>
                  <div class="search_videos_time">${toMinutes(item.durationms)}</div>
              </dt>
              <dd>
                  <div class="search_videos_title c_000 f14 f-thide">${item.title}</div>
                  <div class="c_666 f12 f-thide"><span>${item.type==0?'':"by\n"}</span>${item.creator[0].userName}<span>${!(item.creator[1])?"":("/"+item.creator[1].userName)}</span></div>
              </dd>
          </dl>
          `
    })
  } else if (type == 1006) { //歌词
    totalNum = res.data.result.songCount; //总数
    let songs = res.data.result.songs;
    $('#se_list').html()
    songs.forEach((item, index) => {
      $('#se_list')[0].innerHTML += `
          <dl class="search_songs f12 c_333">
              <dt class="fx_sb">
                  <div class="play_icon" onclick="play_icon_cl(${index},${item.id})"></div>
                  <div class="song_name">${item.name}</div>
                  <div class="search_songer f-thide">${item.ar[0].name}<span>${!(item.ar[1])?"":("/"+item.ar[1].name)}<span></div>
                  <div class="search_zjName f-thide">《${item.al.name}》</div>
                  <div class="search_songs_time">${toMinutes(item.dt)}</div>
              </dt>
              <dd>
                  <div class="search_songs_lyrics"></div>
                  <div class="search_songs_action">展开<span class="search_songs_icon"></span></div>
              </dd>
          </dl>
          `
      let lyrics = item.lyrics
      lyrics.forEach((it, ix) => {
        $('.search_songs_lyrics')[index].innerHTML += `
              <p>${it}</p>
          `
      })
    })
    let isOpenDom = $('.search_songs_action')
    isOpenDom.each((index, item) => {
      item.onclick = function () {
        $('.search_songs_icon').eq(index).toggleClass('search_songs_icon_close')
        $(item.previousElementSibling).toggleClass('search_songs_lyrics_open')
      }
    })

  } else if (type == 1000) { //歌单
    totalNum = res.data.result.playlistCount; //总数
    let playlists = res.data.result.playlists;
    $('#se_list').html()
    playlists.forEach((item, index) => {
      $('#se_list')[0].innerHTML += `
        <tr class="se_playlists fx_sb">
            <td class="fx_fs fx_3">
                <div class="play_icon" onclick="play_playlist(${item.id})"></div>
                <img src="${item.coverImgUrl}?param=50y50">
                <span class="f-thide">${item.name}</span>
            </td>
            <td class="fx_1 fx_sa">
                <div>${item.trackCount}首</div>
                <div class="f-thide">${item.creator.nickname}</div>
            </td>
            <td class="fx_1 fx_sa">
                <div>收藏： ${item.bookCount}</div>
            </td>
            <td class="fx_1 fx_sa"><div>收听：<span>${item.playCount}</span></div></td>
        </tr> 
        `
    })
  } else if (type == 1009) { //主播电台
    totalNum = res.data.result.djRadiosCount; //总数
    let djRadios = res.data.result.djRadios;
    $('#se_list')[0].innerHTML = `
      <h3>声音主播</h3>
      <hr>
    `
    djRadios.forEach((item, index) => {
      $('#se_list')[0].innerHTML += `
      <dl class="search_djRadios fl" onclick="goToDjradioDetail(${item.id})">
        <dt class="search_djRadios_dt fx_sa">
            <img src="${item.picUrl}?param=200y200" alt="">
        </dt>
        <dd class="search_djRadios_dt">
            <div class="search_djRadios_title f14 c_33 f-thide fx_fs">${item.name}</div>
            <div class="search_djRadios_detail f-thide">
              <span>by</span>
              <span>${item.dj.nickname}</span>
              <span class="genger_icon genger_icon_${item.dj.gender}">
              </span>
            </div>
        </dd>
    </dl>`
    })
  } else if (type == 1002) { //用户
    totalNum = res.data.result.userprofileCount; //总数
    let userprofiles = res.data.result.userprofiles
    $('#se_list').html()
    userprofiles.forEach((item, index) => {
      $('#se_list')[0].innerHTML += `
        <tr class="se_userprofiles fx_sb">
            <td class="fx_sb">
                <img src="${item.avatarUrl}?param=50y50">
            </td>
            <td class="se_userprofiles_l">
                <div class="se_userprofiles_nickname f14 c_333">${item.nickname} <div class="genger_icon genger_icon_${item.gender}"></div></div>
                <div class="f-thide c_999">${item.signature}</div>
            </td>
            <td class="fx_1 fx_sa">
                <div>歌单： ${item.playlistCount}</div>
            </td>
            <td class="fx_1 fx_sa">
              <div>粉丝：${toWan(item.followeds)}</div>
            </td>
        </tr> 
        `
    })
  }
  $('.songCount').html(totalNum); //查询的数量
  localStorage.setItem('totalNum',totalNum)
  localStorage.setItem('typeID',type)
  cre_Page(type,offset/30)
}
async function play_icon_cl(i,id,isVip=8){
    if (isVip == 1) {
      alert(`本歌曲为VIP歌曲，请开通VIP后再播放！`)
      return
    }
    play_video(id)
    $('.play_icon').each((index,item)=>{
      $(item).removeClass('play_icon_cl')
      if(index==i) $(item).addClass('play_icon_cl')
    })
} 